package com.tbit.uqbike.client.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tbit.uqbike.client.dao.core.H5LoginConfigDao;
import com.tbit.uqbike.client.exception.BaseException;
import com.tbit.uqbike.client.pojo.newEntity.H5LoginConfig;
import com.tbit.uqbike.client.service.OtherLoginService;
import com.tbit.uqbike.client.service.WeixinPayConfigService;
import com.tbit.uqbike.client.util.H5Login.H5GenSignUtil;
import com.tbit.uqbike.client.util.MD5Util;
import com.tbit.uqbike.client.util.OrderNoUtil;
import com.tbit.uqbike.client.util.TenpayUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.security.NoSuchAlgorithmException;
import java.util.Date;

/**
 * @Author: chen
 * @Description:
 * @Date: 创建于 10:06 2023/10/17
 */
@Service
@Slf4j
public class OtherLoginServiceImpl implements OtherLoginService {

    @Autowired
    private WeixinPayConfigService weixinPayConfigService;

    @Autowired
    private H5LoginConfigDao h5LoginConfigDao;

    @Override
    public String getSign(Integer accountId) {
        H5LoginConfig h5LoginConfig = h5LoginConfigDao.getByAccountId(accountId);
        if (h5LoginConfig == null) {
            throw new BaseException("-2006", "该品牌微信支付未配置");
        }
        JSONObject jsonObject = new JSONObject();
        String traceId = OrderNoUtil.getOrderNO((long) TenpayUtil.buildRandom(6), 1001);
        String timestamp = DateUtil.format(new Date(), "yyyyMMddHHmmssSSS");
        String version = "1.0";
        String businessType = "8";
        try {
            String sign = MD5Util.md5DigestAsHex(h5LoginConfig.getAppId() + businessType + traceId + timestamp +
                    traceId + version + h5LoginConfig.getSecret());
            jsonObject.put("sign", sign);
        } catch (NoSuchAlgorithmException e) {
            log.error(e.getMessage(), e);
        }
        jsonObject.put("traceId", traceId);
        jsonObject.put("timestamp", timestamp);
        jsonObject.put("version", version);
        return jsonObject.toJSONString();
    }

    @Override
    public String getPhone(String accessToken, String userInformation, Integer accountId) {
        H5LoginConfig h5LoginConfig = h5LoginConfigDao.getByAccountId(accountId);
        if (h5LoginConfig == null) {
            throw new BaseException("-2006", "该品牌微信支付未配置");
        }
        String traceId = OrderNoUtil.getOrderNO((long) TenpayUtil.buildRandom(6), 1001);
        String timestamp = DateUtil.format(new Date(), "yyyyMMddHHmmssSSS");
        String url = h5LoginConfig.getUrl();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("token", accessToken);
        jsonObject.put("sign", H5GenSignUtil.testGenSign(h5LoginConfig.getAppId(), traceId, timestamp, accessToken, "1.0", h5LoginConfig.getPrivateKey(), h5LoginConfig.getPublicKey()));
        jsonObject.put("userInformation", userInformation);
        String result = HttpRequest.post(url).header("interfaceVersion", "1.0")
                .header("appId", h5LoginConfig.getAppId()).header("traceId", traceId).header("timestamp", timestamp)
                .header("businessType", "8").header("Content-Type", "application/json").body(jsonObject.toJSONString())
                .execute().body();
        log.info("H5登录返回值=" + result);
        JSONObject object = JSON.parseObject(result);
        if ("103000".equalsIgnoreCase(object.getString("resultCode"))) {
            String msisdn = object.getJSONObject("data").getString("msisdn");
            String phone = H5GenSignUtil.testAesDecode(msisdn, h5LoginConfig.getSecret());
            return phone;
        }
        return null;
    }


}
